package br.com.hstar.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.dozer.util.mapping.MapperIF;


public class DozerHelper {

	/**
	 * An helper method that execute a loop in an list and convert all
	 * object into this list to a new object specified by <code>destinationClass</code>
	 * param.
	 * 
	 * @param mapper An Dozer mapper implementation
	 * @param sourceList The source list
	 * @param destinationClass The destination class
	 * 
	 * @return An new list, with mapped object inside then.
	 */
	public static List mapList(MapperIF mapper, List sourceList, Class destinationClass) {
		// Create the new result list
		List resultList = new ArrayList(sourceList.size());
		
		// Get a iterator
		Iterator iterator = sourceList.iterator();
		// while not is the end of iterator
		while (iterator.hasNext()) {
			// Convert the object
			Object sourceObject = iterator.next();
			Object destinationObject = mapper.map(sourceObject, destinationClass);
			resultList.add(destinationObject);
		}
		
		// return the new list, filled with data or not
		return resultList;
	}

	/**
	 * An helper method that execute a loop in a set and convert all
	 * object into this set to a new object specified by <code>destinationClass</code>
	 * param.
	 * 
	 * @param mapper An Dozer mapper implementation
	 * @param sourceSet The source set
	 * @param destinationClass The destination class
	 * 
	 * @return An new set, with mapped object inside them.
	 */
	public static Set mapSet(MapperIF mapper, Set sourceSet, Class destinationClass) {
		// Create the new result list
		Set resultSet = new HashSet(sourceSet.size());
		
		// Get a iterator
		Iterator iterator = sourceSet.iterator();
		// while not is the end of iterator
		while (iterator.hasNext()) {
			// Convert the object
			Object sourceObject = iterator.next();
			Object destinationObject = mapper.map(sourceObject, destinationClass);
			resultSet.add(destinationObject);
		}
		
		// return the new list, filled with data or not
		return resultSet;
	}
	
	/**
	 * Converts a data object map of PersistentObjects to 
	 * UI objects that implement the GWT type IsSerializable.
	 * 
	 * @param mapper			The Dozer mapper object
	 * @param sourceMap			The source map to convert from
	 * @param destinationClass	The type to convert the data classes to.
	 * @return	The resulting map that is serializable towards the browser.
	 */
	public static Map convertMap( MapperIF mapper, Map sourceMap, Class destinationClass ) {
		// Create the new result list
		Map resultMap = new HashMap(sourceMap.size());
		
		// Get a iterator
		Iterator iterator = sourceMap.keySet().iterator();
		// while not is the end of iterator
		while (iterator.hasNext()) {
			// Convert the object
			Object keyObject = iterator.next();
			Object sourceObject = sourceMap.get( keyObject );
			Object destinationObject = mapper.map(sourceObject, destinationClass);
			resultMap.put( keyObject, destinationObject );
		}
		
		// return the new map, filled with data or not
		return resultMap;
	}

	/**
	 * Default constructor
	 */
	private DozerHelper() {
		super();
	}

}

